package com.agger.harmonyosthemeseditor.userinterface;

import com.agger.harmonyosthemeseditor.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.configuration.DeviceCapability;

import java.util.List;

public class ImageSwiper extends PageSliderProvider
{
    private boolean Complete = false;
    private Object[] objects;

    //数据实体类
    public static class DataItem
    {
        int mResId;
        public DataItem(int resId) {
            mResId = resId;
        }
    }
    // 数据源，每个页面对应list中的一项
    private List<DataItem> list;
    private Context mContext;

    public ImageSwiper(List<DataItem> list, Context context)
    {
        this.list = list;
        this.mContext = context;
        this.objects = new Object[list.size()];
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i)
    {
        if(!Complete)
        {
            final DataItem data = list.get(i);
            DisplayAttributes displayAttributes = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes();
            DirectionalLayout directionalLayout = new DirectionalLayout(mContext);
            directionalLayout.setHeight(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            directionalLayout.setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            directionalLayout.setAlignment(LayoutAlignment.TOP|LayoutAlignment.HORIZONTAL_CENTER);
            directionalLayout.setMarginLeft((int)((20f*displayAttributes.scalDensity)+0.5f));
            directionalLayout.setMarginRight((int)((20f*displayAttributes.scalDensity)+0.5f));
            Image image = new Image(mContext);
            image.setHeight((int)((180f*displayAttributes.scalDensity)+0.5f));
            image.setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            DeviceCapability deviceCapability = mContext.getResourceManager().getDeviceCapability();
            if(deviceCapability.width < 360)image.setScaleMode(Image.ScaleMode.ZOOM_START);
            else image.setScaleMode(Image.ScaleMode.ZOOM_CENTER);
            image.setPixelMap(data.mResId);
            image.setAlpha((ResourceTool.getColor(mContext, ResourceTable.Color_dawnlighti_color_mode,-1)==-16777216)?0.8f:1f);
            directionalLayout.addComponent(image);
            componentContainer.addComponent(directionalLayout);
            objects[i] = directionalLayout;
            if(i == (list.size()-1))Complete = true;
            return directionalLayout;
        }
        else return objects[i];
    }
    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o)
    {
        //componentContainer.removeComponent((Component) o);
    }
    @Override
    public boolean isPageMatchToObject(Component component, Object o)
    {
        //可添加具体处理逻辑
        return true;
    }
}
